﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQtoStudents
{
    class Student
    {
        private string firstName;

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        private string middleName;

        public string MiddleName
        {
            get { return middleName; }
            set { middleName = value; }
        }

        private string lastName;

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public Student(string name)
        {
            string[] arr = name.Split(' ');
            firstName = arr[0];
            middleName = arr[1];
            lastName = arr[2];
            age = int.Parse(arr[3]);
        }

        public override string ToString()
        {
            return firstName + " " + middleName + " " + lastName;
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add(new Student("Ivan Petrov Georgiev 18"));
            students.Add(new Student("Georgi Georgiev Petrov 32"));
            students.Add(new Student("Petar Georgiev Ivanov 22"));
            students.Add(new Student("Wesko Iliev Marinov 48"));
            students.Add(new Student("Kiro Kirov Kirov 98"));
            students.Add(new Student("Kiro Kirov Irov 98"));
            students.Add(new Student("Kiro Airov Tirov 98"));

            var linqVar =
                from Student st in students
                where st.FirstName.CompareTo(st.LastName)>0
                select st.ToString();
            Console.WriteLine("First query result:");
            foreach (var res in linqVar)
            {
                Console.WriteLine(res);
            }

            var ageQuery =
                from Student st in students
                where st.Age >= 18 && st.Age <= 24
                select new { FirstName = st.FirstName, LastName = st.LastName };

            Console.WriteLine("\nSecond query result:");
            foreach (var res in ageQuery)
            {
                Console.WriteLine(res.FirstName + " " + res.LastName);
            }

           // var sorted = students.OrderByDescending(st => st.FirstName + " " + st.LastName);
            var sorted = students.OrderByDescending(st => st.FirstName).ThenByDescending(st => st.LastName);

            Console.WriteLine("\nSorted list:");
            foreach (var item in sorted)
            {
                Console.WriteLine(item.ToString());
            }

            var linqSorted =
                from Student st in students
                orderby st.FirstName descending,st.LastName descending
                
                where true
                select st;

            Console.WriteLine("\nLINQ sorted list:");
            foreach (var item in linqSorted)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }
}
